using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;


using Material.Entity;
using Material.Object;

namespace Material.App
{
    public partial class FrmPembBarang : Form
    {
        private string connStr = ConfigurationManager.ConnectionStrings["dbconnection"].ToString();
        private TblBarang tblBarang;
        private TblBarangEntity tblBarangEntity;

        //private TblTransPembBarangHDR tblTransPembBarangHDR;
        private TblTransPembBarangHDREntity tblTransPembBarangHDREntity;

        //TblTransPemBarangDTL tblTransPemBarangDTL;
        TblTransPemBarangDTLEntity tblTransPemBarangDTLEntity;

        public static int idTrans = 0;
        private int idBarang = 0;

        public FrmPembBarang()
        {
            InitializeComponent();
            EnabledButtons(true, true, true, true);
            ClearTextbox();

            dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Green;
            dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;

            //dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            //dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //dataGridView1.AllowUserToResizeColumns = false;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Kondisi Untuk Disable dan Enable Button
        /// </summary>
        /// <param name="blbtnNew"></param>
        /// <param name="blbtnCancel"></param>
        /// <param name="blbtnSave"></param>
        /// <param name="blbtnPrint"></param>
        private void EnabledButtons(bool blbtnNew, bool blbtnAddDetail, bool blbtnSave,bool btnFind)
        {
            //btnNew.Enabled = blbtnNew;
            btnAddDetail.Enabled = blbtnAddDetail;
            btnDelete.Enabled = blbtnSave;
            btnFindBarang.Enabled = btnFind;
            //btnPrint.Enabled = blbtnPrint;

        }

        /// <summary>
        /// Clear Semua TextBox
        /// </summary>
        private void ClearTextbox()
        {
            txtKelompokBarang.Text = string.Empty;
            txtSatuanBarang.Text = string.Empty;
            txtNamaBarang.Text = string.Empty;
        }


        private void btnFindBarang_Click(object sender, EventArgs e)
        {
            FrmFindBarangDua frm = new FrmFindBarangDua();
            frm.ShowDialog();
            if ( frm.idBarang.Trim().Length > 0)
            {
                //tblBarangEntity = new TblBarangEntity(connStr);
                //tblBarang = new TblBarang();
                //tblBarang.Id = Convert.ToInt32(frm.idBarang);
                //idBarang = Convert.ToInt32(frm.idBarang);
                //tblBarang = tblBarangEntity.GetDataByID(tblBarang);

                //txtNamaBarang.Text = tblBarang.NamaBarang;
                //txtKelompokBarang.Text = tblBarang.TblKelompok.Kelompok;
                //txtSatuanBarang.Text = tblBarang.TblSatuan.Satuan;

                //tblTransPembBarangHDR = new TblTransPembBarangHDR();
                //tblTransPembBarangHDR.IdBarang = idBarang;
                //tblTransPembBarangHDREntity = new TblTransPembBarangHDREntity(connStr);

                //tblTransPembBarangHDR = tblTransPembBarangHDREntity.GetDataByIDBarang(tblTransPembBarangHDR);
                //idTrans = tblTransPembBarangHDR.IdTrans;
                //FrmPembBarangDTL.idTransHDR = idTrans;
                //FrmPembBarangDTL.idBarang = idBarang;
                //BindGrid();
            }
        }

        private void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (txtNamaBarang.Text.Trim().Length != 0)
            {
                FrmPembBarangDTL.stateInput = "Add";
                if (dataGridView1.Rows.Count == 0)
                    FrmPembBarangDTL.isHeader = true;
                else
                    FrmPembBarangDTL.isHeader = false;

                FrmPembBarangDTL frm = new FrmPembBarangDTL();
                frm.ShowDialog();
                BindGrid();
            }
            else
                MessageBox.Show("Barang harus dipilih terlebih dahulu", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //int i = 0;
            //tblTransPembBarangHDR = new TblTransPembBarangHDR();
            //tblTransPembBarangHDREntity = new TblTransPembBarangHDREntity(connStr);
            //tblTransPembBarangHDR = tblTransPembBarangHDREntity.GetMaxIDTrans();
            //tblTransPembBarangHDR.IdTrans = tblTransPembBarangHDR.IdTrans; 
            //tblTransPembBarangHDR.IdBarang = idBarang;
            //tblTransPembBarangHDR.Created = DateTime.Now;
            //tblTransPembBarangHDR.CreatedBy = "admin";
            //tblTransPembBarangHDR.Modified=DateTime.Now;
            //tblTransPembBarangHDR.ModifiedBy="admin";

            //i = tblTransPembBarangHDREntity.Insert(tblTransPembBarangHDR);

            //if (i != -1)
            //{
            //    MessageBox.Show("sukses");
            //    btnAddDetail.Enabled = true;
            //    btnFindBarang.Enabled = false;
            //    btnSave.Enabled = false;
            //    //frmPembBarangDTL = new FrmPembBarangDTL();
            //    FrmPembBarangDTL.idTransHDR = tblTransPembBarangHDR.IdTrans;

                
            //}
            //else
            //    MessageBox.Show("tida");
            ////tblTransPembBarangHDR = tblTransPembBarangHDREntity.GetMaxIDTrans();
            ////idTrans = tblTransPembBarangHDR.IdTrans;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            
        }

        private void BindGrid()
        {
            //tblTransPemBarangDTL = new TblTransPemBarangDTL();
            //tblTransPemBarangDTLEntity = new TblTransPemBarangDTLEntity(connStr);

            //tblTransPemBarangDTL.IdTransHDR = idTrans;
            //dataGridView1.AutoGenerateColumns = false;
            //dataGridView1.DataSource = tblTransPemBarangDTLEntity.GetDataByIDTransHDR(idTrans.ToString()).Tables[0];
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //tblTransPemBarangDTL = new TblTransPemBarangDTL();
            //tblTransPemBarangDTLEntity = new TblTransPemBarangDTLEntity(connStr);
            //if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
            //{

            //    switch (e.ColumnIndex)
            //    {
            //        case 7:
            //            tblTransPemBarangDTL.IdTransDTL = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
            //            tblTransPemBarangDTL = tblTransPemBarangDTLEntity.GetDataByID(tblTransPemBarangDTL);
            //            FrmPembBarangDTL.stateInput = "Edit";
            //            FrmPembBarangDTL.idTransDTL = tblTransPemBarangDTL.IdTransDTL;
            //            FrmPembBarangDTL frm = new FrmPembBarangDTL();
            //            FrmPembBarangDTL.isHeader = false;
            //            frm.ShowDialog();
            //            BindGrid();
            //            break;

            //        case 8:
            //            DialogResult dr = MessageBox.Show("Data akan dihapus..", "Pesan", MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
            //            if (dr == DialogResult.Yes)
            //            {
            //                tblTransPemBarangDTL.IdTransDTL = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
            //                int i = tblTransPemBarangDTLEntity.Delete(tblTransPemBarangDTL);
            //            }
            //            BindGrid();
                        
            //            break;
            //    }
            //}
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //if (txtNamaBarang.Text.Trim().Length != 0)
            //{
            //    int i = 0;
            //    DialogResult dr = MessageBox.Show("Data akan dihapus..", "Pesan", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            //    if (dr == DialogResult.Yes)
            //    {

            //        tblTransPemBarangDTL = new TblTransPemBarangDTL();
            //        tblTransPemBarangDTLEntity = new TblTransPemBarangDTLEntity(connStr);
            //        tblTransPemBarangDTL.IdTransHDR = idTrans;
            //        i = tblTransPemBarangDTLEntity.DeleteByIdHDR(tblTransPemBarangDTL);
            //        if (i != -1)
            //        {
            //            tblTransPembBarangHDR = new TblTransPembBarangHDR();
            //            tblTransPembBarangHDREntity = new TblTransPembBarangHDREntity(connStr);

            //            tblTransPembBarangHDR.IdTrans = idTrans;
            //            i = tblTransPembBarangHDREntity.Delete(tblTransPembBarangHDR);
            //            idTrans = 0;
            //        }

            //        if (i != -1)
            //        {
            //            MessageBox.Show("Data berhasil dihapus..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //            ClearTextbox();
            //            BindGrid();
            //        }
            //        else
            //            MessageBox.Show("Data tidak berhasil dihapus..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Error);

            //    }
            //}
            //else
            //    MessageBox.Show("Barang harus dipilih terlebih dahulu", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);


            


        }
    }
}